package ru.spbau.bytecode.geom;

import java.awt.Point;


public class Segment {

	public final Point a;
	public final Point b;
	public final Line line;
	public static final double eps = 1e-9;
	
	public Segment(Point p1, Point p2) {
		a = p1;
		b = p2;
		line = new Line(this);
	}
	
	public boolean intersects(Segment s) {
		Point p = s.line.getIntersection(line);
		if (p == null) {
			return false;
		}
		
		return this.containsPoint(p) && s.containsPoint(p);
	}
	
	public Point getIntersection(Segment s) {
		Point p = s.line.getIntersection(line);
		if (p == null || !this.containsPoint(p) || !s.containsPoint(p)) {
			return null;
		} else {
			return p;
		}
	}
	
	public boolean containsPoint(Point p) {
		return ((p.x - a.x) * (p.x - b.x) <= 0 &&
				(p.y - a.y) * (p.y - b.y) <= 0);
	}
}
